﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;
using System.IO;

namespace AuditWeb.Controllers
{ 
    public class AttachmentController : Controller
    {
        private AuditDBEntities db = new AuditDBEntities();
        IAnnualEventBL iaebl = new AnnualEventBL();
        IArchiveBL iabl = new ArchiveBL();

        //
        // GET: /Attachment/
        [Authorize(Roles = "区县管理员")]
        public ViewResult Index(string id)
        {
            Archive archive = db.Archives.Single(a => a.ID == id);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(archive.AnnualEventID);
            ViewBag.AnnualEventId = archive.AnnualEventID;
            ViewBag.CountyName = db.Counties.SingleOrDefault(c => c.Code == archive.CountyCode).Name;
            ViewBag.ArchiveID = id;

            var attachments = archive.Attachments;
            return View(attachments.ToList());
        }

        //
        // GET: /Attachment/Details/5

        //public ViewResult Details(string id)
        //{
        //    Attachment attachment = db.Attachments.Single(a => a.Title == id);
        //    return View(attachment);
        //}

        //
        // GET: /Attachment/Create

        //public ActionResult Create()
        //{
        //    ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "AnnualEventID");
        //    return View();
        //} 

        //
        // POST: /Attachment/Create

        [HttpPost]
        [Authorize(Roles = "区县管理员")]
        public ActionResult Create(string id, HttpPostedFileBase file)
        {
            Archive archive = db.Archives.Single(a => a.ID == id);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(archive.AnnualEventID);
            ViewBag.AnnualEventId = archive.AnnualEventID;
            ViewBag.CountyName = db.Counties.SingleOrDefault(c => c.Code == archive.CountyCode).Name;

            if (file != null && file.ContentLength > 0)
            {
                Attachment attachment = new Attachment();
                db.Attachments.AddObject(attachment);
                /*Upload real file*/
                var fileName = Path.GetFileName(file.FileName);// +Path.GetExtension(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/attachments"), archive.AnnualEventID, archive.Subject.Name, archive.ID);
                path = Path.Combine(path, "files");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = Path.Combine(path, fileName);
                attachment.ArchiveID = id;
                attachment.Title = fileName;
                attachment.FilePath = "~/Content/attachments/"+archive.AnnualEventID+"/"+archive.Subject.Name+"/"+archive.ID+"/files/"+fileName;
                db.SaveChanges();
                file.SaveAs(path);
            }
            
            return RedirectToAction("Index", new { id = id });
        }
        
        //
        // GET: /Attachment/Edit/5
 
        //public ActionResult Edit(string id)
        //{
        //    Attachment attachment = db.Attachments.Single(a => a.Title == id);
        //    ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "AnnualEventID", attachment.ArchiveID);
        //    return View(attachment);
        //}

        ////
        //// POST: /Attachment/Edit/5

        //[HttpPost]
        //public ActionResult Edit(Attachment attachment)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Attachments.Attach(attachment);
        //        db.ObjectStateManager.ChangeObjectState(attachment, EntityState.Modified);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.ArchiveID = new SelectList(db.Archives, "ID", "AnnualEventID", attachment.ArchiveID);
        //    return View(attachment);
        //}

        //
        // GET: /Attachment/Delete/5
 [Authorize(Roles = "区县管理员")]
        public ActionResult Delete(string id, string title)
        {
            //Attachment attachment = db.Attachments.Single(a => a.Title == id);
            //return View(attachment);
            Archive archive = db.Archives.Single(a => a.ID == id);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(archive.AnnualEventID);
            ViewBag.AnnualEventId = archive.AnnualEventID;
            ViewBag.CountyName = db.Counties.SingleOrDefault(c => c.Code == archive.CountyCode).Name;

            Attachment attachment = db.Attachments.Single(a => a.Title == title && a.ArchiveID==id);

            //Directory.Delete(Server.MapPath(attachment.FilePath));
            System.IO.File.Delete(Server.MapPath(attachment.FilePath));
            
            db.Attachments.DeleteObject(attachment);
            /*Delete true file*/
            

            db.SaveChanges();
            return RedirectToAction("Index", new {id=id });
        }

        //
        // POST: /Attachment/Delete/5

        //[HttpPost, ActionName("Delete")]
        //public ActionResult DeleteConfirmed(string id)
        //{            
        //    Attachment attachment = db.Attachments.Single(a => a.Title == id);
        //    db.Attachments.DeleteObject(attachment);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}